package com.robusta.app.domain.spreadsheet;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class ModelMatchers {
    public static Matcher<Model.Field> withName(final String name) {
        return new TypeSafeMatcher<Model.Field>() {
            @Override
            public boolean matchesSafely(Model.Field field) {
                return name.equals(field.getFieldName());
            }

            @Override
            public void describeTo(Description description) {
                description.appendText(String.format("with name: %s", name));
            }
        };
    }

    public static Matcher<Model.Field> withValue(final String value) {
        return new TypeSafeMatcher<Model.Field>() {
            @Override
            public boolean matchesSafely(Model.Field field) {
                return value.equals(field.getFieldValue());
            }

            @Override
            public void describeTo(Description description) {
                description.appendText(String.format("with value: %s", value));
            }
        };
    }

    public static Matcher<Model.Field> keyField() {
        return withKeyOrNonKey(true);
    }

    public static Matcher<Model.Field> nonKeyField() {
        return withKeyOrNonKey(false);
    }


    private static Matcher<Model.Field> withKeyOrNonKey(final boolean isKey) {
        return new TypeSafeMatcher<Model.Field>() {
            @Override
            public boolean matchesSafely(Model.Field field) {
                return isKey == field.isKey();
            }

            @Override
            public void describeTo(Description description) {
                description.appendText(String.format("%s field", isKey ? "key" : "non-key"));
            }
        };
    }
}
